#!/bin/bash

BEGIN=1
END=$1
NUM_PROC=$2
OUT_DIR=$3
OUT_FILE=$4
ALGS=$5

if [[ $5 == "" ]] 
then
  echo "Usage: times_to_run num_cores out_dir out_file algs"
  exit -1
fi 

rm $OUT_FILE

for ((a=$BEGIN; a <= END ; a++)) 
  do 
  ./randSched.py $NUM_PROC .
  for sched in $ALGS; 
    do 
    ./simulator -j ${NUM_PROC}_cores -s $sched -o ${OUT_DIR}/run-${a}-${sched} >> ${OUT_FILE} 2>&1
    if [[ $? -eq 0 ]]
    then
      echo run-${a}-${sched}-good >> $OUT_FILE
      mv ${OUT_DIR}/run-${a}-${sched} ${OUT_DIR}/run-${a}-${sched}-good
    else
      echo run-${a}-${sched}-bad >> $OUT_FILE
      mv ${OUT_DIR}/run-${a}-${sched} ${OUT_DIR}/run-${a}-${sched}-bad
    fi
  done 
done

numNegLax=`grep -c NegativeLaxity $OUT_FILE` 
numIdle=`grep -c IdleTime $OUT_FILE`
numInvalid=`grep -c InvalidSchedulerState $OUT_FILE` 
numSuccess=`grep -c completion $OUT_FILE`

echo "Successful runs: $numSuccess"
echo "Negative Laxity errors: $numNegLax" 
echo "Idle Processor errors: $numIdle"
echo "Scheduler/Job File errors: $numInvalid"
echo "Total results: " $((numNegLax + numIdle + numInvalid + numSuccess))